package com.yida.gwj.report.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.yida.common.core.constant.SecurityConstants;
import com.yida.common.core.domain.R;
import com.yida.common.core.utils.CheckRemoteServiceResultUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.service.RemoteGwjConfigService;
import com.yida.gwj.common.valid.Update;
import com.yida.gwj.report.domain.GwjSysCalculateRate;
import com.yida.gwj.report.domain.dto.GwjSysCalculateRateDetailDTO;
import com.yida.gwj.report.domain.dto.GwjSysCalculateRateListDTO;
import com.yida.gwj.report.domain.request.GwjSysCalculateRateListRequest;
import com.yida.gwj.report.domain.request.GwjSysCalculateRateRequest;
import com.yida.gwj.report.service.IGwjSysCalculateRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统结息Controller
 *
 * @author lqq
 * @date 2024-02-28
 */
@Api(tags = "系统结息")
@RestController
@RequestMapping("/calculateRate")
public class GwjSysCalculateRateController extends BaseController {
    @Resource
    private IGwjSysCalculateRateService gwjSysCalculateRateService;

    @Resource
    private RemoteGwjConfigService remoteGwjConfigService;

    private final String AUTO_CALCULATE_RATE = "auto_calculate_rate";

    /**
     * 查询系统结息列表
     */
    @PostMapping("/list")
    @RequiresPermissions("gwj:report:calculateRate:list")
    @ApiOperation(value = "系统结息-列表", response = GwjSysCalculateRateListDTO.class)
    public TableDataInfo list(@RequestBody GwjSysCalculateRateListRequest request) {
        startPage();
        List<GwjSysCalculateRate> list = gwjSysCalculateRateService.selectGwjSysCalculateRateList(BeanUtil.copyProperties(request, GwjSysCalculateRate.class));
        return getDataDtoTable(list, GwjSysCalculateRateListDTO.class);
    }

    /**
     * 导出系统结息列表
     */
    @PostMapping("/export")
    @ApiOperation(value = "系统结息-导出")
    @RequiresPermissions("gwj:report:calculateRate:export")
    @Log(title = "系统结息", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, GwjSysCalculateRate gwjSysCalculateRate) {
        List<GwjSysCalculateRate> list = gwjSysCalculateRateService.selectGwjSysCalculateRateList(gwjSysCalculateRate);
        ExcelUtil<GwjSysCalculateRate> util = new ExcelUtil<GwjSysCalculateRate>(GwjSysCalculateRate.class);
        util.exportExcel(response, list, "系统结息数据");
    }

    /**
     * 获取系统结息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "系统结息-详情")
    @RequiresPermissions("gwj:report:calculateRate:query")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(BeanUtil.copyProperties(gwjSysCalculateRateService.selectGwjSysCalculateRateById(id), GwjSysCalculateRateDetailDTO.class));
    }

    /**
     * 新增系统结息
     */
    @PostMapping
    @ApiOperation(value = "系统结息-新增")
    @RequiresPermissions("gwj:report:calculateRate:add")
    @Log(title = "系统结息", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated @RequestBody GwjSysCalculateRateRequest request) {
        if (ObjUtil.isNotNull(
                gwjSysCalculateRateService.selectGwjSysCalculateRate(new GwjSysCalculateRate() {{
                    setCalculateTime(request.getCalculateTime());
                    setAreaId(request.getAreaId());
                }})
        )) {
            return error("当前地区结算日期已存在");
        }
        return toAjax(gwjSysCalculateRateService.insertGwjSysCalculateRate(BeanUtil.copyProperties(request, GwjSysCalculateRate.class)));
    }

    /**
     * 修改系统结息
     */
    @PutMapping
    @ApiOperation(value = "系统结息-编辑")
    @RequiresPermissions("gwj:report:calculateRate:edit")
    @Log(title = "系统结息", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated(Update.class) @RequestBody GwjSysCalculateRateRequest request) {
        GwjSysCalculateRate rate = gwjSysCalculateRateService.selectGwjSysCalculateRate(new GwjSysCalculateRate() {{
            setCalculateTime(request.getCalculateTime());
            setAreaId(request.getAreaId());
        }});
        if (ObjUtil.isNotNull(rate) && rate.getId() != request.getId()) {
            return error("当前地区结算日期已存在");
        }
        return toAjax(gwjSysCalculateRateService.updateGwjSysCalculateRate(BeanUtil.copyProperties(request, GwjSysCalculateRate.class)));
    }

    /**
     * 删除系统结息
     */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "系统结息-删除")
    @RequiresPermissions("gwj:report:calculateRate:remove")
    @Log(title = "系统结息", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gwjSysCalculateRateService.deleteGwjSysCalculateRateByIds(ids));
    }

    /**
     * 自动结息配置查询
     */
    @ApiOperation(value = "系统结息-自动结息配置查询")
    @GetMapping("/getAutoConfig")
    public AjaxResult getAutoConfig() {
        R<String> r = remoteGwjConfigService.innerGetConfigKey(AUTO_CALCULATE_RATE, SecurityConstants.INNER);
        if (CheckRemoteServiceResultUtils.isCodeFail(r)) {
            return AjaxResult.error(r.getMsg());
        }
        return AjaxResult.success("查询成功", r.getData());
    }

    /**
     * 自动结息配置修改
     */
    @ApiOperation(value = "系统结息-自动结息配置修改")
    @PutMapping("/editAutoConfig")
    public AjaxResult editAutoConfig() {
        R<String> val = remoteGwjConfigService.innerGetConfigKey(AUTO_CALCULATE_RATE, SecurityConstants.INNER);
        if (CheckRemoteServiceResultUtils.isCodeFail(val)) {
            return AjaxResult.error(val.getMsg());
        }
        String configValue = val.getData();
        if (Boolean.parseBoolean(configValue)) {
            configValue = "false";
        } else {
            configValue = "true";
        }
        R r = remoteGwjConfigService.innerEditGwjConfig(AUTO_CALCULATE_RATE, configValue, SecurityConstants.INNER);
        if (CheckRemoteServiceResultUtils.isCodeFail(r)) {
            return AjaxResult.error(r.getMsg());
        }
        return AjaxResult.success();
    }
}
